﻿using Leestar54.WeChat.WebAPI;
using Leestar54.WeChat.WebAPI.Modal;
using OznerOA.BLL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace OznerOA.UI
{
    public partial class FrmBindWechatApi : FrmBase
    {
        private WechatBLLAPI wechatBLL = WechatBLLAPI.GetInstance();
        #region 构造函数
        public FrmBindWechatApi()
        {
            InitializeComponent();
            lblCaption.Text = "使用手机微信扫码登录";
            lblTip.Text = "登录微信需要配合手机使用";
            this.Load += FrmBindWechatApi_Load;
        }
        #endregion
        #region 窗体事件
        #region 窗体加载
        private void FrmBindWechatApi_Load(object sender, EventArgs e)
        {
            //绑定二维码获取成功的事件
            wechatBLL.GetWechatClient().GetLoginQrCodeComplete += FrmBindWechatApi_GetLoginQrCodeComplete;
            //绑定用户扫描完成的事件
            wechatBLL.GetWechatClient().CheckScanComplete += FrmBindWechatApi_CheckScanComplete;
            //绑定登录成功事件
            wechatBLL.GetWechatClient().LoginComplete += FrmBindWechatApi_LoginComplete;
            //绑定联系人获取完成事件
            wechatBLL.GetContactCompleteHandle += WechatBLL_GetContactCompleteHandle;
            //启动Wechat
            StartWechat();
        }
        #endregion
        #region 窗体关闭事件
        private void FrmBindWechatApi_FormClosed(object sender, FormClosedEventArgs e)
        {
            //取消绑定二维码获取成功的事件
            wechatBLL.GetWechatClient().GetLoginQrCodeComplete -= FrmBindWechatApi_GetLoginQrCodeComplete;
            //取消绑定用户扫描完成的事件
            wechatBLL.GetWechatClient().CheckScanComplete -= FrmBindWechatApi_CheckScanComplete;
            //取消绑定登录成功事件
            wechatBLL.GetWechatClient().LoginComplete -= FrmBindWechatApi_LoginComplete;
            //取消绑定联系人获取完成事件
            wechatBLL.GetContactCompleteHandle -= WechatBLL_GetContactCompleteHandle;
        }
        #endregion
        #endregion
        #region 启动Wechat
        private void StartWechat()
        {
            if (wechatBLL.LoginUser == null)
            {
                //启动微信线程
                wechatBLL.StartClient();
            }
            else
            {
                panelLogin.Visible = false;
                panelMain.Visible = true;
                //已经登录微信
                pictureHead.Image = wechatBLL.LoginUserImage;
                lblNickname.Text = wechatBLL.LoginUser.NickName;
                gridControl1.DataSource = wechatBLL.Contracts.Values;
            }
        }
        #endregion
        #region 微信事件
        #region 获取联系人完成事件
        private void WechatBLL_GetContactCompleteHandle(object sender, TEventArgs<List<Contact>> e)
        {
            gridControl1.DataSource = e.Result;
            CloseWaiting();
        }
        #endregion
        #region 微信登录完成事件
        private void FrmBindWechatApi_LoginComplete(object sender, TEventArgs<Leestar54.WeChat.WebAPI.Modal.User> e)
        {
            logBLL.Debug("登录成功!");
            panelLogin.Visible = false;
            panelMain.Visible = true;
            lblNickname.Text = e.Result.NickName;
            ShowWaiting("正在拉取联系人信息...");
        }
        #endregion
        #region 微信扫码检测事件
        private void FrmBindWechatApi_CheckScanComplete(object sender, TEventArgs<Image> e)
        {
            logBLL.Debug("用户已经扫码");
            lblCaption.Text = "已扫描二维码";
            lblTip.Text = string.Empty;
            pictureEdit1.Image = e.Result;
            pictureHead.Image = e.Result;
        }
        #endregion
        #region 获取二维码完成事件
        private void FrmBindWechatApi_GetLoginQrCodeComplete(object sender, TEventArgs<Image> e)
        {
            logBLL.Debug("已获取登陆二维码");
            pictureEdit1.Image = e.Result;
        }
        #endregion

        #endregion
        #region 退出登录按钮点击
        private void btnSwitchAccount_Click(object sender, EventArgs e)
        {
            wechatBLL.logout();
            this.Close();
        }
        #endregion
    }
}